package com.epam.kulibaba.helpers.testSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;

import com.epam.kulibaba.components.catalog.ProductsCatalog;
import com.epam.kulibaba.components.compare.panelLevelUp.PanelLevelUp;
import com.epam.kulibaba.components.compareTable.CompareTable;
import com.epam.kulibaba.components.productDescription.ProductDescription;

public class ProductDescriptionHelper {

	public static void goToCompare(ProductsCatalog productsCatalog) {
		Reporter.log("Go to Compare");
		productsCatalog.getCompareLink().click();
	}

	public static  HashMap<String,String> getProductOptions(ProductsCatalog productCatalog,
			int productNumber, ProductDescription itemDescription,
			PanelLevelUp panelLevelUp) {
		Reporter.log("getProduct "+productNumber+ " options");
		HashMap<String, String> itemOptions = null;
		ProductsListHelper.goToProductPage(productCatalog, 1);
		itemOptions = ProductDescriptionHelper.getDescription(itemDescription);
		PanelLevelUp.goToCategoryLevelUp(panelLevelUp);
		return itemOptions;
	}

	public static HashMap<String, String> getDescription(
			ProductDescription productDescription) {
		HashMap<String, String> productDescriptions = new HashMap<String, String>();
		List<WebElement> rowDescriptions = productDescription
				.getItemDescription().findElements(By.cssSelector("div.row"));
		for (WebElement row : rowDescriptions) {
			String name = row.findElement(By.cssSelector("span.pr")).getText()
					.toLowerCase();
			String value = row.findElement(By.cssSelector("span.val"))
					.getText().toLowerCase();
			productDescriptions.put(name, value);
		}
		/*
		 * for (Map.Entry me : productDescriptions.entrySet()) {
		 * System.out.println(me.getKey() + " " + me.getValue()); }
		 */
		return productDescriptions;
	}

	public static void checkDescriptionEqualsCompareDescription(
			HashMap<String, String> productDescriptions,
			HashMap<String, String> productDescriptionsCompare) {
		Reporter.log("check DescriptionEqualsCompareDescription");
		for (Entry<String, String> entry : productDescriptions.entrySet()) {
			String key = entry.getKey();
			Assert.assertEquals(
					entry.getValue()
							.equals(productDescriptionsCompare.get(key)), true);
		}
	}

	public static HashMap<String, String> productDescriptionsInCompare(
			CompareTable compareTable, int productNumber) {
		HashMap<String, String> productCompareDescription = new HashMap<String, String>();
		List<WebElement> rowCompareDescriptions = compareTable
				.getCompareTable()
				.findElements(
						By.xpath("//tr[@class = ''] | //tr[@class = 'different']"));
		for (WebElement row : rowCompareDescriptions) {
			String name = row.findElement(By.xpath(".//td[1]")).getText()
					.toLowerCase();
			String value = row.findElement(By.xpath(".//td[2]")).getText()
					.toLowerCase();
			productCompareDescription.put(name, value);
		}
		System.out.println("-------------");
		/*
		 * for (Map.Entry me : productCompareDescription.entrySet()) {
		 * System.out.println(me.getKey() + " " + me.getValue()); }
		 */
		return productCompareDescription;
	}

	public static void checkColorDifferentDescriptions(CompareTable compareTable) {
		Reporter.log("check different options");
		List<WebElement> rowCompareDescriptions = compareTable
				.getCompareTable()
				.findElements(
						By.xpath("//tr[@class = ''] | //tr[@class = 'different']"));
		for (WebElement row : rowCompareDescriptions) {			
			if (!row.findElement(By.xpath(".//td[2]"))
					.getText()
					.toLowerCase()
					.equalsIgnoreCase(
							row.findElement(By.xpath(".//td[3]")).getText()
									.toLowerCase())) {
				System.out.println(row.findElement(By.xpath(".//td[2]"))
						.getText());
				System.out.println(row.findElement(By.xpath(".//td[3]"))
						.getText());
				Assert.assertEquals(row.getAttribute("class"), "different");
			}
		}
	}
	
	public static void checkLinksInSpecialOffers(ProductsCatalog productCatalog,
			int productNumber){
		ProductsListHelper.goToProductPage(productCatalog, productNumber);
		
	}

}
